<template>
  <div class="home">
    <!-- 头部搜索 -->
    <van-nav-bar fixed title="登入" class="page-nav-bar" >
      <template #title>
        <van-button to="/search" class="search-btn" icon="search" type="primary">搜索</van-button>
      </template>
    </van-nav-bar>
    <!-- /头部搜索 -->
    <!-- Tab 标签页 -->
    <van-tabs sticky offset-top="1.22rem" class="home-tab" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channelId='item.id' />
      </van-tab>
      <template #nav-right >
        <div class="placeholder-box"></div>
        <div @click="showPopup" class="right-menu">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <!--/ Tab 标签页 -->
    <van-popup class="editPopup" v-model="isShowEditPopup" close-icon-position="top-left" closeable  position="bottom" :style="{ height: '90%' }">
      <channel-edit @onGo="onGo" @setActive="active--" :channels="channels" :active="active"/>
    </van-popup>
  </div>
</template>

<script>
import { getUserSelfChannels } from '@/api/user'
import { Toast } from 'vant'
import ArticleList from '@/views/Home/components/ArtucleList'
import ChannelEdit from './components/ChannelEdit'
import { getStorage } from '@/utils/storage'
export default {
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },

  created () {
    this.loadUserSelfChannels()
  },

  methods: {
    showPopup () {
      this.isShowEditPopup = true
    },
    async loadUserSelfChannels () {
      try {
        const res = await getUserSelfChannels()
        const storageData = getStorage('MY-CHANNELS')
        if (!this.$store.state.user && storageData) {
          this.channels = storageData
          return false
        }
        console.log(res)
        this.channels = res.channels
      } catch (err) {
        Toast.fail('网络异常')
      }
    },
    onGo (e) {
      this.active = e
      this.isShowEditPopup = false
    }
  }
}
</script>
<style scoped lang='less'>
.home{
  padding-top: 1.22rem;
  .search-btn{
    width: 555px;
    height: 64px;
    border-radius: 32px;
    background:rgba(255, 255, 255, .2) ;
    border: none;
  }
  .van-icon-search{
    color: #fff;
  }
  /deep/.van-nav-bar__title{
    max-width: unset;
  }
  /deep/.home-tab{
    .van-tab{
      min-width: 200px;
      border-right: 1px solid #edeff3;
      height: 82px;
      .van-tab__text{
        font-size: 28px;
        color: #777;
      }
    }
    .van-tabs__wrap{
      height: 82px;
      border-bottom: 1px solid #edeff3;
    }
    .van-tab--active{
      .van-tab__text{
        font-size: 30px;
        color: #333;
      }
    }
    .van-tabs__line{
      width: 31px;
      height: 6px;
      background: #3296FA;
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav--line{
      padding: 0;
    }
    .right-menu{
      width: 66px;
      height: 82px;
      background-color: #fff;
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      i{
        font-size: 40px;
      }
      &::before{
        content: '';
        width: 2px;
        height: 58px;
        position: absolute;
        top: 12px;
        left: 0;
        background-image: url(~@/assets/images/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    .placeholder-box{
      width: 64px;
      flex-shrink: 0;
    }
  }
  .editPopup{
padding-top: 100px;
  }
}
</style>
